/*
 * Copyright (c) Jan Šlahora (${USER}) - 2012.
 * Created in IntelliJ IDEA 10.5
 */

package statistics.Asonance;

import java.util.ArrayList;
import java.util.Collections;

/*
*   File AsonanceFixniPosun 
*   in package statistics.Asonance
*   was created by slaha
*   on 31.3.12 at 22:57.
*
*/
public class AsonanceFixniPosun extends IAsonance {

  private static int posun;
  private String jmeno;
  private ArrayList<Double> hodnoty = new ArrayList<>();

  public AsonanceFixniPosun() {
    super();
  }

  public AsonanceFixniPosun(ArrayList<String> texty, int posun, String jmeno) throws Exception {
    super(texty);
    if (!(posun > 0)) {
      throw new Exception("Can't compute asonantion for zero or less value");
    }

    this.posun = posun;
    this.jmeno = jmeno;

    run();
  }


  public void add(AsonanceFixniPosun asonance) {
    aTest.add(asonance.hodnoty, asonance.jmeno);
  }

  public int getPosun() {
    return posun;
  }

  public boolean remove(String jmeno) {
    return aTest.remove(jmeno);
  }

  @Override
  public void run() {
    ArrayList<IAsonance.SpocitaniAsonance> al = new ArrayList<>();

    for (String s : texty) {
      al.add(new SpocitaniAsonance(s, posun, jmeno));
      try {
        al.get((al.size() - 1)).join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    boolean hotovo;
    do {
      hotovo = true;
      for (IAsonance.SpocitaniAsonance sa : al) {
        if (!sa.isHotovo()) {
          hotovo = false;
        }
      }
    } while (!hotovo);


    for (int i = 0; i < al.size(); i++) {
      double hodnota = al.get(i).getHodnoty().get(0);
      hodnoty.add(hodnota);

    }
    aTest.add(hodnoty, jmeno);
  }

  private class SpocitaniAsonance extends IAsonance.SpocitaniAsonance {
    private int posun;
    private String jmeno;

    public SpocitaniAsonance(String text, int posun, String jmeno) {
      super(text);
      this.posun = posun;
      this.jmeno = jmeno;
      run();
      hotovo = true;
    }

    @Override
    public void run() {
      ArrayList<Character> vokaly = dejVokaly(text.toCharArray());

      if (vokaly.size() > (25+this.posun)) {
        vokaly.subList(25+this.posun, vokaly.size()).clear();
      }

      int pocetStejnych = 0;

      int pocetKroku = vokaly.size() - this.posun;
      for (int x = 0; x < pocetKroku; x++) {
        if (vokaly.get(x).equals(vokaly.get(x+posun))) {
          pocetStejnych++;
        }
      }
      //System.out.println(jmeno + ": " +pocetStejnych + " / " + pocetKroku);
      double pomer = ((double) pocetStejnych) / (double) pocetKroku;

      hodnoty.add(pomer);
     // System.out.println(pomer);
    }


    public boolean isHotovo() {
      return hotovo;
    }



  }
}

